package com.heima.admin.v1.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.admin.entity.AdSensitive;
import com.heima.admin.entity.SensitivePageDto;
import com.heima.admin.service.AdSensitiveService;

import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.model.admin.dtos.SensitiveDto;
import io.swagger.annotations.Api;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

/*
* 敏感词管理*/
@RestController
@Api(tags = "敏感词管理接口")
public class SensitiveController {
    @Autowired
    private AdSensitiveService adSensitiveService;

    /*
    * 敏感词添加*/
    @PostMapping("/api/v1/sensitive/save")
    public ResponseResult save(@RequestBody SensitiveDto dto){

        AdSensitive sensitive=new AdSensitive();
       // 不为空
      if (dto==null || StringUtils.isEmpty(dto.getSensitives())){
          return ResponseResult.error(AppHttpCodeEnum.PARAM_REQUIRE);
      }
        LambdaQueryWrapper<AdSensitive> queryWrapper=new LambdaQueryWrapper<>();
      queryWrapper.eq(AdSensitive::getSensitives,dto.getSensitives());
        AdSensitive adSensitive = adSensitiveService.getOne(queryWrapper);
       //判断重复
        if (adSensitive != null) {
            return ResponseResult.error(AppHttpCodeEnum.DATA_EXIST);
        }
        //添加
        BeanUtils.copyProperties(dto,sensitive);
        adSensitiveService.save(sensitive);
        return ResponseResult.ok();

    }


    /*
    * 分页查询*/
    @PostMapping("/api/v1/sensitive/list")
    private PageResult<AdSensitive> list(@RequestBody SensitivePageDto dto){

        return  adSensitiveService.pageQuery(dto);

    }

    /*
    * 删除敏感词*/
      @DeleteMapping("/api/v1/sensitive/del/{id}")
    private ResponseResult delete(@PathVariable("id") Integer id){

           adSensitiveService.removeById(id);
           return  ResponseResult.ok();
      }

      /*
      * 修改敏感词*/
    @PutMapping("/api/v1/sensitive/update")
    private ResponseResult update(@RequestBody SensitiveDto dto ){
        AdSensitive adSensitive=new AdSensitive();

       if (StringUtils.isNotEmpty(dto.getSensitives()) ||dto.getId()!=null){
                BeanUtils.copyProperties(dto,adSensitive);
           adSensitiveService.updateById(adSensitive);
           return ResponseResult.ok();
       }else {
           return  ResponseResult.error(AppHttpCodeEnum.UPDATE_ERROR);
       }
    }
}
